<template>
  <div class="section">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      hide-required-asterisk
    >
      <el-form-item label="商品名称" prop="name">
        <el-input
          class="name"
          v-model="form.name"
          placeholder="请输入商品名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品价格" prop="price">
        <el-input
          class="price"
          v-model="form.price"
          placeholder="请输入商品价格"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品描述" prop="desc">
        <el-input
          class="desc"
          v-model="form.desc"
          placeholder="请输入商品描述"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="submit-btn" type="primary" @click="submitForm('form')"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang="less"></style>

<script>
import apiUrl from "../utils/apis";
export default {
  data() {
    return {
      isEdit: false,
      pid: "",
      form: {
        name: "",
        price: "",
        desc: "",
      },
      rules: {
        name: [{ required: true, message: "请输入名称" }],
        price: [
          {
            required: true,
            message: "请输入价格",
            type: "number",
            transform(value) {
              return Number(value);
            },
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      let that = this;

      that.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.isEdit) {
            let url = apiUrl.updateProduct + that.pid;
            that.$axios
              .put(url, {
                name: that.form.name,
                price: Number(that.form.price),
                desc: that.form.desc,
              })
              .then((res) => {
                console.log(res);
                if (res.success) {
                  that.$router.go(-1);
                }
              })
              .catch((err) => {
                console.log(err.response);
              });
          } else {
            let url = apiUrl.addProduct;
            that.$axios
              .post(url, {
                name: that.form.name,
                price: Number(that.form.price),
                desc: that.form.desc,
              })
              .then((res) => {
                console.log(res);
                if (res.success) {
                  that.$router.go(-1);
                }
              })
              .catch((err) => {
                console.log(err.response);
              });
          }
        } else {
          return false;
        }
      });
    },
    getExistData(id) {
      this.$axios
        .get(apiUrl.detailProduct + this.pid)
        .then((res) => {
          console.log(res);
          this.form = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  mounted() {
    this.pid = this.$route.query.id;
    this.isEdit = typeof this.pid === "undefined" ? false : true;
    if (this.isEdit) {
      this.getExistData(this.pid);
    }
  },
};
</script>
